﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsPayStartDAL.cs
/// 作  者：kidy
/// 时  间：2007.01.15
/// 说  明：ClsPayStartDAL类用于销售管理信息相关处理
/// </summary>
public class ClsPayStartDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有主表信息
    /// </summary>
    /// <param name="Type">类型</param>
    /// <returns>返回主表信息数据集</returns>
    public DataSet GetEarlyPayFundMains(string sType, string sCurUserId)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetEarlyPayFundM", con);
        dat.SelectCommand.CommandType = CommandType.StoredProcedure;

        dat.SelectCommand.Parameters.Add("@u_log_id", SqlDbType.NVarChar, 30);
        dat.SelectCommand.Parameters["@u_log_id"].Value = sCurUserId;
        dat.SelectCommand.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        dat.SelectCommand.Parameters["@type"].Value = sType;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 获取应付期初单或采购退款单从表信息CREATE BY KIDY ON 20070116
    /// </summary>
    /// <returns>返回从表信息数据集</returns>
    public DataSet GetEarlyPayFundChilds(string sType,string sPay_no)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetEarlyPayFundD", con);
        dat.SelectCommand.CommandType = CommandType.StoredProcedure;

        dat.SelectCommand.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        dat.SelectCommand.Parameters["@type"].Value = sType;
        dat.SelectCommand.Parameters.Add("@pay_no", SqlDbType.NVarChar, 30);
        dat.SelectCommand.Parameters["@pay_no"].Value = sPay_no;
       
        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 确认应付期初单
    /// </summary>
    /// <returns>操作是否成功</returns>
    public Boolean AuditEarlyPayOutFund(string sPay_No, int State, string Auditor, string Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AuditEarlyPayOutFund", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@pay_no", SqlDbType.NVarChar, 30);
        com.Parameters["@pay_no"].Value = sPay_No;
        com.Parameters.Add("@is_audit", SqlDbType.Int, 2);
        com.Parameters["@is_audit"].Value = State;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 30);
        com.Parameters["@auditor"].Value = Auditor;
        com.Parameters.Add("@type", SqlDbType.Int, 2);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 改变订单从表的状态
    /// </summary>
    /// <param name="Dm_No">订单单号</param>
    /// <param name="N_No">从表单号</param>
    /// <param name="State">操作状态(0确认，1取消确认即终止，2删除即作废，3彻底删除)</param>
    /// <param name="Type">类型</param>
    /// <returns>操作是否成功</returns>
    public Boolean UpdatePayOutFundChildState(string Dm_No, string N_No, int State, string Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdatePayOutFundChildState", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@dm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@dm_no"].Value = Dm_No;
        com.Parameters.Add("@n_no", SqlDbType.NVarChar, 30);
        com.Parameters["@n_no"].Value = N_No;
        com.Parameters.Add("@dm_state", SqlDbType.Int, 4);
        com.Parameters["@dm_state"].Value = State;
        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 新增 应付期初 主表记录
    /// </summary>CREATE BY KIDY ON 20070116    
    /// strType, strPay_no, dPayDate, strProvide, strDept, strWriter,dOper_Date, strRemark
    public Boolean AddEarlyPayFund(string sType, string sPay_no, DateTime dPayDate, string sProvide, string sDept,
        string sWorker, string sBalanceType, string sWriter, DateTime dOper_Date, string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddEarlyPayFund", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = sType;
        com.Parameters.Add("@pay_no", SqlDbType.NVarChar, 30);
        com.Parameters["@pay_no"].Value = sPay_no;
        com.Parameters.Add("@pay_date", SqlDbType.DateTime, 8);
        com.Parameters["@pay_date"].Value = dPayDate;
        com.Parameters.Add("@provide_id", SqlDbType.NVarChar, 30);
        com.Parameters["@provide_id"].Value = sProvide;
        com.Parameters.Add("@dept_id", SqlDbType.NVarChar, 30);
        com.Parameters["@dept_id"].Value = sDept;
        com.Parameters.Add("@worker", SqlDbType.NVarChar, 30);
        com.Parameters["@worker"].Value = sWorker;
        com.Parameters.Add("@BalanceType", SqlDbType.NVarChar, 30);
        com.Parameters["@BalanceType"].Value = sBalanceType;
        com.Parameters.Add("@oper", SqlDbType.NVarChar, 30);
        com.Parameters["@oper"].Value = sWriter;
        com.Parameters.Add("@oper_date", SqlDbType.DateTime, 8);
        com.Parameters["@oper_date"].Value = dOper_Date;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = sRemark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 新增应付期初从表记录
    /// </summary>
    /// <returns>返回操作是否成功</returns>
    public Boolean AddEarlyPayFundChild(string sType, string sPay_no, DateTime dPay_date, string sIm_no, decimal dPrice,
        decimal dQty, string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddEarlyPayFundD", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = sType;
        com.Parameters.Add("@pay_no", SqlDbType.NVarChar, 30);
        com.Parameters["@pay_no"].Value = sPay_no;
        com.Parameters.Add("@pay_date", SqlDbType.DateTime, 8);
        com.Parameters["@pay_date"].Value = dPay_date;
        com.Parameters.Add("@Im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@Im_no"].Value = sIm_no;
        com.Parameters.Add("@price", SqlDbType.Decimal, 28);
        com.Parameters["@price"].Value = dPrice;
        com.Parameters.Add("@qty", SqlDbType.Decimal, 28);
        com.Parameters["@qty"].Value = dQty;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = sRemark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改应付期初单记录
    /// </summary>
    /// <returns>返回操作是否成功</returns>
    public Boolean UpdateEarlyPayFundM(string sType, string sPay_no, DateTime dPayDate,string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdateEarlyPayFundM", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = sType;
        com.Parameters.Add("@pay_no", SqlDbType.NVarChar, 30);
        com.Parameters["@pay_no"].Value = sPay_no;
        com.Parameters.Add("@payDate", SqlDbType.DateTime, 8);
        com.Parameters["@payDate"].Value = dPayDate;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = sRemark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改应付期初从表记录
    /// </summary>
    /// <returns>返回操作是否成功</returns>
    public Boolean UpdateEarlyPayFundChild(string sType, string sPay_no,string sIm_no,
        decimal dQty,decimal dPrice,string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdateEarlyPayFund", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = sType;
        com.Parameters.Add("@pay_no", SqlDbType.NVarChar, 30);
        com.Parameters["@pay_no"].Value = sPay_no;       
        com.Parameters.Add("@Im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@Im_no"].Value = sIm_no;
        com.Parameters.Add("@qty", SqlDbType.Decimal, 28);
        com.Parameters["@qty"].Value = dQty;
        com.Parameters.Add("@price", SqlDbType.Decimal, 28);
        com.Parameters["@price"].Value = dPrice;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = sRemark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 删除应付期初单主表或者明细表记录 
    /// </summary>
    /// <param>CREATE BY KIDY ON 20070116</param>
    /// <param name="Is_Visible"></param>
    /// <returns>返回删除是否成功</returns>
    public Boolean DeleteEarlyPayFund(string sType, string sPay_no,string sIm_no,int iState)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteEarlyPayFund", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@type", SqlDbType.NVarChar, 30);
        com.Parameters["@type"].Value = sType;
        com.Parameters.Add("@pay_no", SqlDbType.NVarChar, 30);
        com.Parameters["@pay_no"].Value = sPay_no;       
        com.Parameters.Add("@Im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@Im_no"].Value = sIm_no;        
        com.Parameters.Add("@state", SqlDbType.Int, 1);
        com.Parameters["@state"].Value = iState;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 根据查询状态获取 应付期初 主表信息
    /// </summary>CREATE BY KIDY ON 20070116
    /// <param name="Type">类型</param>
    /// <returns>返回主表信息数据集</returns>
    public DataSet GetOrderInformationByStatu(string sUserId, string sTable, string sType, int iQueryType)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetOrderByEarlyPayFund", con);
        dat.SelectCommand.CommandType = CommandType.StoredProcedure;

        dat.SelectCommand.Parameters.Add("@u_log_id", SqlDbType.VarChar, 30);
        dat.SelectCommand.Parameters["@u_log_id"].Value = sUserId;
        dat.SelectCommand.Parameters.Add("@table", SqlDbType.VarChar, 30);
        dat.SelectCommand.Parameters["@table"].Value = sTable;
        dat.SelectCommand.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        dat.SelectCommand.Parameters["@type"].Value = sType;
        dat.SelectCommand.Parameters.Add("@queryType", SqlDbType.Int, 2);
        dat.SelectCommand.Parameters["@queryType"].Value = iQueryType;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }
}
